using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace TR0217.ControlEx
{
    [ToolboxBitmap(typeof(ColorPickerCtrl), "ColorPickerCtrl.bmp")]
	public partial class ColorPickerCtrl : UserControl
	{
		Color _selectedColor;
		public Color SelectedColor
		{
			get { return Color.FromArgb((int)Math.Floor(255f*m_opacity), _selectedColor);; }
			set
			{
				m_opacity = (float)value.A / 255f;
				value = Color.FromArgb(255, value);
				_colorWheel.SelectedColor = value;
				if (_colorTable.ColorExist(value) == false)
					_colorTable.SetCustomColor(value);
				_colorTable.SelectedItem = value;
				_opacitySlider.Percent = m_opacity;
			}
		}

		public ColorPickerCtrl()
		{
			InitializeComponent();
            _selectedColor = _colorTable.SelectedItem;

			_colorTable.SelectedIndexChanged += new EventHandler(OnColorTableSelectionChanged);
			_colorWheel.SelectedColorChanged += new EventHandler(OnColorWheelSelectionChanged);
			_opacitySlider.SelectedValueChanged += new EventHandler(OnOpacityValueChanged);
			_eyedropColorPicker.SelectedColorChanged += new EventHandler(OnEyeDropperSelectionChanged);
			_colorSample.Paint += new PaintEventHandler(OnColorSamplePaint);
		}
		void OnEyeDropperSelectionChanged(object sender, EventArgs e)
		{
			_colorWheel.SelectedColor = _eyedropColorPicker.SelectedColor;
		}
		float m_opacity = 1;
		void OnOpacityValueChanged(object sender, EventArgs e)
		{
			m_opacity = Math.Max(0, _opacitySlider.Percent);
			m_opacity = Math.Min(1, _opacitySlider.Percent);
			_colorSample.Refresh();
			UpdateInfo();
		}
		void OnColorWheelSelectionChanged(object sender, EventArgs e)
		{
			Color selcol = _colorWheel.SelectedColor;
			if (selcol != null && selcol != _selectedColor)
			{
				_selectedColor = selcol;
				_colorSample.Refresh();
				if (lockColorTable == false && selcol != _colorTable.SelectedItem)
					_colorTable.SetCustomColor(selcol);
			}
			UpdateInfo();
		}
		void UpdateInfo()
		{
			Color c = Color.FromArgb((int)Math.Floor(255f*m_opacity), _selectedColor);
			string info = string.Format("{0} aRGB({1}, {2}, {3}, {4})", _colorWheel.SelectedHSLColor.ToString(), c.A, c.R, c.G, c.B);
			_infoLabel.Text = info;
		}
		void OnColorSamplePaint(object sender, PaintEventArgs e)
		{
			Rectangle r = _colorSample.ClientRectangle;
			r.Inflate(-4,-4);

			int width = r.Width;
			r.Width /= 2;
			
			Color c = Color.FromArgb((int)Math.Floor(255f*m_opacity), _selectedColor);
			SolidBrush b = new SolidBrush(c);
			e.Graphics.FillRectangle(b, r);

			r.X += r.Width;

			e.Graphics.FillRectangle(Brushes.White, r);
			c = Color.FromArgb(255, _selectedColor);
			b = new SolidBrush(c);
			e.Graphics.FillRectangle(b, r);
		}

		bool lockColorTable = false;
		void OnColorTableSelectionChanged(object sender, EventArgs e)
		{
			Color selcol = (Color)_colorTable.SelectedItem;
			if (selcol != null && selcol != _selectedColor)
			{
				lockColorTable = true;
				_colorWheel.SelectedColor = selcol;
				lockColorTable = false;
				_colorSample.Invalidate();
			}
		}
	}
}
